/*
 * AutoCompleteRecord.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.indexeddictionary;

import java.util.Comparator;
import java.util.Locale;

import android.util.Log;

import de.studiorutton.gerengdic.databaseaccess.LanguageAccess.IndexPositionAccess.IndexAll;

public abstract class AutoCompleteRecord {
	private final static String TAG = "learningwords";
	
	public final static int TYPE_HEADLINE = 0;
	public final static int TYPE_CONTENT = 1;

	private final static int GREATER_THAN = 1;
	private final static int EQUALS = 0;
	private final static int LESS_THAN = -1;

	public int type;
	public int errors;
	
	public AutoCompleteRecord( int type, int errors ) {
		this.type = type;
		this.errors = errors;
	}
	
	public String getKey() {
		return "";
	}
	
	public IndexAll getIndexAll() {
		return null;
	}
	
	static class ACRComparator implements Comparator<AutoCompleteRecord> {

		public int compare(AutoCompleteRecord arg1, AutoCompleteRecord arg2) {

			if ( arg1.type == arg2.type  ) {
				if (arg1.errors < arg2.errors)
					return LESS_THAN;
				else if (arg1.errors > arg2.errors)
					return GREATER_THAN;
				else
					if ( arg1.type == TYPE_CONTENT ) {
						String keyOne = arg1.getKey();
						String keyTwo = arg2.getKey();
						
						String s1 = keyOne.toLowerCase( Locale.GERMAN ).replace("ä", "ae" ).replace("ö", "oe").replace("ü", "ue");
						String s2 = keyTwo.toLowerCase( Locale.GERMAN ).replace("ä", "ae" ).replace("ö", "oe").replace("ü", "ue");
						return s1.compareTo( s2 );
					} else
						return EQUALS;
			} else {
				if ( arg1.type == TYPE_HEADLINE && arg2.type == TYPE_CONTENT ) {
//					Log.d(TAG, "Comparing headline(" + arg1.errors +") and content(" + arg2.errors + " - " + arg2.indexAll.indexRecord.getKey() + ")");
					return compareHeadlineAndContent( arg1, arg2 );
				}
				else {
//					Log.d(TAG, "Comparing content(" + arg1.errors + " - " + arg1.indexAll.indexRecord.getKey() + ") and headline(" + arg1.errors +")");
					return compareContentAndHeadline( arg1, arg2 );
				}
			}
		}
		
		
		public int compareHeadlineAndContent( AutoCompleteRecord arg1, AutoCompleteRecord arg2 ) {
			if ( arg1.errors <= arg2.errors )
				return LESS_THAN;
			else
				return GREATER_THAN;
		}
		
		public int compareContentAndHeadline( AutoCompleteRecord arg1, AutoCompleteRecord arg2 ) {
			if ( arg1.errors < arg2.errors )
				return LESS_THAN;
			else
				return GREATER_THAN;
		}
	}
}
